﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using de.codingfreaks.libs.cfThreadingTools;
using de.codingfreaks.libs.cfThreadingTools.Extensions;
using System.Threading;
using System.Diagnostics;

namespace cfThreadingToolsTest
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            WaitCallback wcbThis = new WaitCallback(FillListView);
            ThreadPool.QueueUserWorkItem(wcbThis);
        }

        private void DoSomethingInOwnThread(object objNull)
        {
            BaseTools.SetPropertyValue(btnTest, "Enabled", false);
            
            BaseTools.SetPropertyValue(btnTest, "Enabled", true);
        }

        private void FillListView(object objNull)
        {
            btnTest.DisableThreadSafe();
            lvwThis.Items.ClearThreadSafe(lvwThis);                        
            lvwThis.SuspendLayoutThreadSafe();
            for (int i = 0; i < 1000; i++)
            {
                ListViewItem litTmp = new ListViewItem(i.ToString());
                litTmp.SubItems.Add(Guid.NewGuid().ToString());
                litTmp.SubItems.Add(Guid.NewGuid().ToString());
                lvwThis.AddItemThreadSafe(litTmp);
            }
            lvwThis.ResumeLayoutThreadSafe();
            btnTest.EnableThreadSafe();
        }

        private void btnBenchST_Click(object sender, EventArgs e)
        {
            Stopwatch swaTmp = new Stopwatch();
            swaTmp.Start();
            btnBenchST.DisableThreadSafe();
            for (int i = 0; i < 10000; i++)
            {
                txtBench.SetTextThreadSafe(Guid.NewGuid().ToString());
            }
            btnBenchST.EnableThreadSafe();
            swaTmp.Stop();
            MessageBox.Show(swaTmp.ElapsedMilliseconds.ToString());
        }

        private void btnBench_Click(object sender, EventArgs e)
        {
            Stopwatch swaTmp = new Stopwatch();
            swaTmp.Start();
            btnBench.Enabled = false;
            for (int i = 0; i < 10000; i++)
            {
                txtBench.Text = Guid.NewGuid().ToString();
            }
            btnBench.Enabled = true;
            swaTmp.Stop();
            MessageBox.Show(swaTmp.ElapsedMilliseconds.ToString());
        }
    }
}
